<template>
  <div class="home">

    <!-- 顶部通栏 -->
    <div class="top-banner">
      <div class="banner-slider">
        <div class="banner-slide" v-for="(slide, index) in bannerSlides" :key="index" :class="{ active: index === currentBannerSlide }">
          <img :src="slide.image" :alt="slide.alt" class="banner-image">
          <div v-if="shouldShowBannerText(slide)" class="banner-content" :class="{ 'text-left': slide.align === 'left', 'text-center': slide.align === 'center', 'text-right': slide.align === 'right' }" :style="bannerContentStyle(slide)">
            <div class="banner-text">
              <h1 class="banner-title" :style="bannerTitleStyle(slide)">{{ slide.title }}</h1>
              <p class="banner-slogan" :style="bannerSloganStyle(slide)">{{ slide.slogan }}</p>
            </div>
          </div>
        </div>
      </div>
      <div class="banner-indicators">
        <span 
          v-for="(slide, index) in bannerSlides" 
          :key="index" 
          class="indicator-dot" 
          :class="{ active: index === currentBannerSlide }"
          @click="goToBannerSlide(index)"
        ></span>
      </div>
    </div>

    <!-- 品牌介绍区 -->
    <section class="brand-intro-section">
      <div class="intro-container">
        <h2 class="section-title">关于我们</h2>
        <div class="title-underline"></div>
        <div class="intro-content">
          <p>绿碳圈是一个致力于推动环保理念的社交平台，我们相信每个人的小小努力都能汇聚成改变世界的力量。</p>
          <p>通过记录碳足迹、参与环保挑战、分享绿色生活，您可以轻松地为地球环境保护贡献力量。</p>
          <p>加入我们，一起创造更美好的未来，让环保成为一种生活方式。</p>
        </div>
      </div>
    
      <!-- 数据概览区 -->
      <div class="overview-container">
        <div class="overview-item no-bg">
          <div class="overview-number artistic-number">{{ carbonReduction }}</div>
          <div class="overview-label">总碳减排量(kg)</div>
        </div>
        <div class="overview-item no-bg">
          <div class="overview-number artistic-number">{{ activeUsers }}</div>
          <div class="overview-label">活跃用户数</div>
        </div>
        <div class="overview-item no-bg">
          <div class="overview-number artistic-number">{{ exchanges }}</div>
          <div class="overview-label">交换成功数</div>
        </div>
        <div class="overview-item no-bg">
          <div class="overview-number artistic-number">{{ entries }}</div>
          <div class="overview-label">贡献词条数</div>
        </div>
      </div>
    </section>

    <!-- 核心功能区 -->
    <section class="core-functions-section">
      <div class="functions-container">
        <div class="section-header">
          <h2 class="section-title">核心功能</h2>
        </div>
        <div class="functions-grid">
          <!-- 碳足迹记录 -->
          <div class="function-card" @click="$router.push('/carbon')">
            <div class="card-content">
              <div class="card-text">
                <h3 class="overlay-title">碳足迹记录</h3>
                <p class="overlay-desc">追踪和管理您的日常碳排放，为环保贡献力量</p>
                <a href="#" class="learn-more">了解更多</a>
              </div>
              <div class="card-image">
                <img src="@/assets/carousel/carousel-home-6.png" alt="碳足迹记录">
              </div>
            </div>
          </div>

          <!-- 环保挑战 -->
          <div class="function-card" @click="$router.push('/challenge')">
            <div class="card-content">
              <div class="card-text">
                <h3 class="overlay-title">环保挑战</h3>
                <p class="overlay-desc">参与有趣的环保挑战，提升环保意识</p>
                <a href="#" class="learn-more">了解更多</a>
              </div>
              <div class="card-image">
                <img src="@/assets/carousel/carousel-home-2.jpg" alt="环保挑战">
              </div>
            </div>
          </div>

          <!-- 物品交换 -->
          <div class="function-card" @click="$router.push('/exchange')">
            <div class="card-content">
              <div class="card-text">
                <h3 class="overlay-title">物品交换</h3>
                <p class="overlay-desc">闲置物品循环利用，实现资源最大化价值</p>
                <a href="#" class="learn-more">了解更多</a>
              </div>
              <div class="card-image">
                <img src="@/assets/carousel/carousel-home-3.jpg" alt="物品交换">
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- 产品/内容区 -->
    <section class="products-section">
      <div class="products-container">
        <div class="section-header">
          <h2 class="section-title">物品交换</h2>
        </div>
        <div class="products-slider-wrapper">
          <!-- 左箭头按钮 -->
          <button class="arrow-btn left-arrow" @click="prevSlide" :disabled="currentIndex === 0">‹</button>
          
          <!-- 轮播容器 -->
          <div class="products-slider">
            <div class="product-card-container" :style="{ transform: `translateX(-${currentIndex * 33.33}%)` }">
              <div class="product-card-item" v-for="(item, index) in exchangeItems" :key="item.id">
                <div class="product-card" :class="{ 'center-card': index === currentIndex + 1 }">
                  <img :src="item.image_url || defaultNewsImg" :alt="item.title">
                  <div class="product-info">
                    <h3 class="product-title">{{ item.title }}</h3>
                    <p class="product-desc">{{ item.description || '暂无描述' }}</p>
                  </div>
                </div>
              </div>
            </div>
          </div>
          
          <!-- 右箭头按钮 -->
          <button class="arrow-btn right-arrow" @click="nextSlide" :disabled="currentIndex >= exchangeItems.length - 3">›</button>
        </div>
        
        <!-- 指示器 -->
        <div class="indicators">
          <span 
            v-for="(item, index) in exchangeItems" 
            :key="index" 
            class="indicator-dot" 
            :class="{ active: index === currentIndex }"
            @click="goToSlide(index)"
          ></span>
        </div>
      </div>
    </section>

    <!-- 物品交换和知识挑战区 (原物品交换区域，现在显示交换后的内容) -->
    <section class="community-news-section">
      <div class="news-container-full-bg">
        <div class="news-bg-image">
          <img src="@/assets/carousel/carousel-home-2.jpg" alt="物品交换和知识挑战">
        </div>
        <div class="news-content-overlay">
          <div class="news-tabs">
            <button class="tab-btn" :class="{ active: activeTab === 'exchange' }" @click="switchTab('exchange')">环保动态</button>
            <button class="tab-btn" :class="{ active: activeTab === 'challenge' }" @click="switchTab('challenge')">我的碳足迹</button>
          </div>
          <div class="news-content-wrapper">
            <!-- 环保动态内容 (原物品交换内容区域，现在显示环保动态) -->
            <transition name="fade" mode="out-in">
              <div v-if="activeTab === 'exchange'" key="exchange" class="news-content-area">
                <div class="news-item" v-for="item in ecoNews.slice(0, 3)" :key="item.post_id" @click="$router.push(`/post/${item.post_id}`)">
                  <img :src="item.cover_url || defaultNewsImg" :alt="item.title" class="news-thumb">
                  <div class="news-text">
                    <h3 class="news-title">{{ item.title }}</h3>
                    <p class="news-meta">{{ item.excerpt || ' ' }}</p>
                  </div>
                </div>
              </div>
            </transition>
            
            <!-- 个人碳足迹图表 (占据整个区域) -->
            <transition name="fade" mode="out-in">
              <div v-if="activeTab === 'challenge'" key="challenge" class="news-content-area carbon-chart-container" ref="carbonChartRef"></div>
            </transition>
          </div>
        </div>
      </div>
    </section>

    
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, computed } from 'vue'
import { getUserStatistics, getHomeBanners } from '@/api/user'
import { getPosts } from '@/api/community'
import { getExchangeItems } from '@/api/exchange'
import { getTrend } from '@/api/carbon'
import * as echarts from 'echarts'
import defaultNewsImg from '@/assets/carousel/carousel-home-1.jpg'
import carouselHome0 from '@/assets/carousel/carousel-home-0.png'
import carouselHome5 from '@/assets/carousel/carousel-home-5.png'
import carouselHome7 from '@/assets/carousel/carousel-home-7.png'


// 数据概览
const carbonReduction = ref(0)
const activeUsers = ref(0)
const exchanges = ref(0)
const entries = ref(0)
const ecoNews = ref<any[]>([])
const sliderRef = ref<HTMLElement | null>(null)
const canPrev = ref(false)
const canNext = ref(false)
const activeTab = ref('exchange')
const exchangeItems = ref<any[]>([])
const carbonChartRef = ref<HTMLElement | null>(null)
let carbonChart: any = null

// 添加轮播相关的响应式数据
const currentIndex = ref(0)

// 顶部横幅轮播相关数据
const currentBannerSlide = ref(0)
const bannerSlides = ref<any[]>([])

// 顶部横幅轮播相关方法
let bannerAutoSlideInterval: ReturnType<typeof setInterval> | null = null

const startBannerAutoSlide = () => {
  if (bannerAutoSlideInterval) {
    clearInterval(bannerAutoSlideInterval)
  }
  
  bannerAutoSlideInterval = setInterval(() => {
    const n = bannerSlides.value.length
    if (n > 0) {
      currentBannerSlide.value = (currentBannerSlide.value + 1) % n
    }
  }, 5000)
}

const stopBannerAutoSlide = () => {
  if (bannerAutoSlideInterval) {
    clearInterval(bannerAutoSlideInterval)
    bannerAutoSlideInterval = null
  }
}

const goToBannerSlide = (index: number) => {
  currentBannerSlide.value = index
}

const bannerContentStyle = (slide: any) => ({ transform: `translate(${Number(slide.offset_x || 0)}px, ${Number(slide.offset_y || 0)}px)` })
const bannerTitleStyle = (slide: any) => ({ color: slide.title_color || '#fff', fontSize: `${Number(slide.title_font_size || 48)}px` })
const bannerSloganStyle = (slide: any) => ({ color: slide.slogan_color || '#fff', fontSize: `${Number(slide.slogan_font_size || 20)}px` })
const shouldShowBannerText = (slide: any) => {
  const hasText = Boolean((slide.title && String(slide.title).trim()) || (slide.slogan && String(slide.slogan).trim()))
  if (typeof slide.show_text === 'boolean') return slide.show_text && hasText
  return hasText
}

// 上一张
const prevSlide = () => {
  if (currentIndex.value > 0) {
    currentIndex.value--
  }
}

// 下一张
const nextSlide = () => {
  // 确保不超过最大索引
  if (currentIndex.value < Math.max(0, exchangeItems.value.length - 3)) {
    currentIndex.value++
  }
}

// 跳转到指定卡片
const goToSlide = (index: number) => {
  // 确保索引在有效范围内
  const maxIndex = Math.max(0, exchangeItems.value.length - 3);
  if (index >= 0 && index <= maxIndex) {
    currentIndex.value = index
  }
}

// 自动轮播
let autoSlideInterval: ReturnType<typeof setInterval> | null = null

const startAutoSlide = () => {
  if (autoSlideInterval) {
    clearInterval(autoSlideInterval)
  }
  
  autoSlideInterval = setInterval(() => {
    // 检查是否到达末尾，如果到达则回到开头
    if (currentIndex.value >= Math.max(0, exchangeItems.value.length - 3)) {
      currentIndex.value = 0
    } else {
      nextSlide()
    }
  }, 5000) // 每5秒自动切换
}

const stopAutoSlide = () => {
  if (autoSlideInterval) {
    clearInterval(autoSlideInterval)
    autoSlideInterval = null
  }
}

// 切换标签页
const switchTab = (tab: string) => {
  activeTab.value = tab
  
  // 如果切换到碳足迹标签页，初始化图表
  if (tab === 'challenge') {
    setTimeout(() => {
      initCarbonChart()
    }, 100)
  }
}

// 暴露给模板使用的变量和方法
defineExpose({
  activeTab,
  exchangeItems,
  switchTab,
  // 顶部横幅轮播相关
  currentBannerSlide,
  bannerSlides,
  goToBannerSlide
})

// 数字动画效果
const animateNumber = (target: number, callback: (value: number) => void) => {
  let current = 0
  const increment = target / 50
  const timer = setInterval(() => {
    current += increment
    if (current >= target) {
      current = target
      clearInterval(timer)
    }
    callback(Math.floor(current))
  }, 30)
}

// 获取用户统计数据
const fetchUserStats = async () => {
  try {
    const response: any = await getUserStatistics()
    if (response.success && response.data) {
      // 动画显示数据
      animateNumber(response.data.carbon_records_count || 0, (value) => {
        carbonReduction.value = value
      })
      animateNumber(response.data.posts_count || 0, (value) => {
        activeUsers.value = value
      })
      animateNumber(response.data.completed_challenges_count || 0, (value) => {
        exchanges.value = value
      })
      // 这里可以添加更多数据项
      entries.value = 1200 // 示例数据
    }
  } catch (error) {
    console.error('获取用户统计数据失败:', error)
  }
}

const updateSliderButtons = () => {
  const el = sliderRef.value
  if (!el) return
  const maxScroll = el.scrollWidth - el.clientWidth
  canPrev.value = el.scrollLeft > 0
  canNext.value = el.scrollLeft < maxScroll - 1
}

const slidePrev = () => {
  const el = sliderRef.value
  if (!el) return
  el.scrollBy({ left: -el.clientWidth, behavior: 'smooth' })
  setTimeout(updateSliderButtons, 300)
}

const slideNext = () => {
  const el = sliderRef.value
  if (!el) return
  el.scrollBy({ left: el.clientWidth, behavior: 'smooth' })
  setTimeout(updateSliderButtons, 300)
}



// 初始化碳足迹图表
const initCarbonChart = () => {
  if (carbonChartRef.value && activeTab.value === 'challenge') {
    // 销毁之前的图表实例
    if (carbonChart) {
      carbonChart.dispose()
    }
    
    // 创建新的图表实例
    carbonChart = echarts.init(carbonChartRef.value)
    
    // 生成数据
    const data: any[] = []
    for (let i = 0; i <= 360; i++) {
      let t = (i / 180) * Math.PI
      let r = Math.sin(2 * t) * Math.cos(2 * t)
      data.push([r, i])
    }
    
    // 图表配置
    const option = {
      title: {
        text: '个人碳足迹统计',
        top: 10, // 将标题放在图表上方
        left: 'center',
        textStyle: {
          color: '#2c3e50',
          fontSize: 16
        }
      },
      legend: {
        data: ['碳足迹'],
        bottom: 10, // 将图例放在图表下方
        left: 'center'
      },
      polar: {
        center: ['50%', '50%'],
        radius: '70%' // 调整极坐标图大小，为标题和图例留出空间
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross'
        }
      },
      angleAxis: {
        type: 'value',
        startAngle: 0
      },
      radiusAxis: {
        min: 0
      },
      series: [
        {
          coordinateSystem: 'polar',
          name: '碳足迹',
          type: 'line',
          showSymbol: false,
          data: data,
          lineStyle: {
            color: '#4CAF50'
          },
          areaStyle: {
            color: '#4CAF50',
            opacity: 0.3
          }
        }
      ],
      animationDuration: 2000
    }
    
    // 设置图表配置
    carbonChart.setOption(option)
  }
}

// 组件挂载时的逻辑
onMounted(() => {
  // 获取用户统计数据
  fetchUserStats()
  
  // 获取物品交换数据
  getExchangeItems({ limit: 10 }).then((resp: any) => {
    const items = resp?.data?.items || []
    exchangeItems.value = items.map((item: any) => ({
      id: item.item_id,
      title: item.title,
      description: item.description || '暂无描述',
      image_url: item.image_url
    }))
  }).catch(() => {})
  
  // 获取碳足迹趋势数据
  getTrend({ days: 7 }).then((resp: any) => {
    // 这里可以处理碳足迹数据，但目前我们使用静态图表
  }).catch(() => {})
  
  getPosts({ order: 'likes', limit: 6 }).then((resp: any) => {
    const posts = resp?.data?.posts || []
    ecoNews.value = posts.map((p: any) => ({
      post_id: p.post_id,
      title: ((p.content || '').split('\n')[0] || '绿色动态').slice(0, 20),
      excerpt: (p.content || '').replace('\r', '').slice(0, 60),
      cover_url: (Array.isArray(p.image_urls) && p.image_urls.length > 0) ? p.image_urls[0] : undefined,
      created_at: p.created_at,
      like_count: p.like_count
    }))
    setTimeout(updateSliderButtons, 0)
  }).catch(() => {})
  if (sliderRef.value) {
    sliderRef.value.addEventListener('scroll', updateSliderButtons)
    window.addEventListener('resize', updateSliderButtons)
    setTimeout(updateSliderButtons, 0)
  }
  
  // 初始化碳足迹图表
  // 只有在 activeTab 为 'challenge' 时才初始化图表
  if (activeTab.value === 'challenge') {
    setTimeout(() => {
      initCarbonChart()
    }, 100)
  }

  // 顶部横幅：使用后端配置，失败则回退本地预设
  getHomeBanners().then((resp: any) => {
    const items = resp?.data?.items || []
    if (items.length) {
      bannerSlides.value = items.map((x: any) => ({
        image: x.image_url,
        alt: x.title || '绿迹环保平台',
        title: x.title ?? '',
        slogan: x.slogan ?? '',
        align: x.align || 'right',
        title_color: x.title_color,
        slogan_color: x.slogan_color,
        title_font_size: x.title_font_size,
        slogan_font_size: x.slogan_font_size,
        offset_x: x.offset_x,
        offset_y: x.offset_y,
        show_text: x.show_text,
      }))
    }
  }).catch(() => {}).finally(() => {
    if (!bannerSlides.value.length) {
      bannerSlides.value = [
        { image: carouselHome0, alt: '绿迹环保平台', title: '绿迹', slogan: '让每一步都有环保温度', align: 'right' },
        { image: carouselHome5, alt: '绿迹环保平台', title: '绿迹', slogan: '让每一步都有环保温度', align: 'right' },
        { image: carouselHome7, alt: '绿迹环保平台', title: '绿迹', slogan: '让每一步都有环保温度', align: 'right' }
      ]
    }
  })

  startAutoSlide()
  startBannerAutoSlide()
})

// 组件卸载时的清理
onUnmounted(() => {
  // 组件卸载时需要清理的逻辑
  const el = sliderRef.value
  if (el) el.removeEventListener('scroll', updateSliderButtons)
  window.removeEventListener('resize', updateSliderButtons)
  
  // 销毁图表实例
  if (carbonChart) {
    carbonChart.dispose()
    carbonChart = null
  }
  
  stopAutoSlide()
  stopBannerAutoSlide()
})
</script>

<style scoped>
.home {
  min-height: 100vh;
  background: #f8f9fa;
}

/* 顶部通栏样式 */
.top-banner {
  height: 563px;
  position: relative;
  overflow: hidden;
}

.banner-slider {
  position: relative;
  width: 100%;
  height: 100%;
}

.banner-slide {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transition: opacity 1s ease-in-out;
}

.banner-slide.active {
  opacity: 1;
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.banner-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 100px;
  color: #fff;
  z-index: 1;
}

.banner-content.text-left { justify-content: flex-start; text-align: left; }
.banner-content.text-center { justify-content: center; text-align: center; }
.banner-content.text-right { justify-content: flex-end; text-align: right; }

.banner-text {
  z-index: 2;
}

.banner-title {
  font-family: 'KaiTi', '楷体', STKaiti, serif;
  font-size: 4rem;
  font-weight: bold;
  margin-bottom: 20px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
  writing-mode: vertical-rl;
  letter-spacing: 5px;
}

.banner-slogan {
  font-family: 'KaiTi', '楷体', STKaiti, serif;
  font-size: 1.5rem;
  color: #f8f9fa;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3);
  writing-mode: vertical-rl;
  letter-spacing: 2px;
}

.banner-indicators {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  z-index: 2;
}

.banner-indicators .indicator-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  transition: all 0.3s ease;
}

.banner-indicators .indicator-dot.active {
  background: #4CAF50;
  transform: scale(1.2);
}

.banner-indicators .indicator-dot:hover {
  background: #45a049;
}

/* 品牌介绍区 */
.brand-intro-section {
  padding: 40px 20px;
  background: url('@/assets/carousel/carousel-home-4.png') center/cover, rgba(248, 249, 250, 0.7);
  background-blend-mode: overlay;
  position: relative;
  overflow: hidden;
}

.brand-intro-section::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.7);
  z-index: 1;
}

.intro-container {
  max-width: 1200px;
  margin: 0 auto;
  text-align: center;
  position: relative;
  z-index: 2;
}

.section-title {
  font-size: 2.5rem;
  font-weight: bold;
  color: #2c3e50;
  margin-bottom: 20px;
}

.title-underline {
  width: 60px;
  height: 3px;
  background: #4CAF50;
  margin: 0 auto 40px;
  border-radius: 2px;
}

.intro-content {
  max-width: 800px;
  margin: 0 auto 60px;
  font-size: 1.1rem;
  color: #6c757d;
  line-height: 1.8;
  position: relative;
  z-index: 2;
}

.intro-content p {
  margin-bottom: 20px;
}

/* 数据概览区 */
.overview-container {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 30px;
  padding: 0 20px 60px;
  position: relative;
  z-index: 2;
}

.overview-item {
  text-align: center;
  padding: 30px;
  border-radius: 15px;
  background: rgba(255, 255, 255, 0.8);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.overview-item.no-bg {
  background: transparent;
  box-shadow: none;
  backdrop-filter: none;
  padding: 15px;
}

.overview-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
}

.overview-item.no-bg:hover {
  transform: none;
  box-shadow: none;
}

.overview-number {
  font-size: 2.5rem;
  font-weight: bold;
  color: #4CAF50;
  margin-bottom: 10px;
}

.artistic-number {
  font-family: 'KaiTi', '楷体', STKaiti, serif;
  font-size: 3rem;
  font-weight: bold;
  background: linear-gradient(135deg, #4CAF50, #2E7D32);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 15px;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.overview-label {
  font-size: 1rem;
  color: #6c757d;
}

.brand-intro-section::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100px;
  background: url('@/assets/carousel/carousel-home-4.png') center bottom/auto 100px repeat-x;
  opacity: 0.1;
  z-index: 1;
}

/* 核心功能区 */
.core-functions-section {
  padding: 40px 20px;
  background: #f8f9fa;
}

.functions-container {
  width: 100%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.section-header {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}

.section-title {
  font-size: 2.5rem;
  font-weight: bold;
  color: #2c3e50;
  margin: 0;
  margin-right: 20px;
}

.title-divider {
  flex: 1;
  height: 2px;
  background: #4CAF50;
  min-width: 50px;
}

.functions-grid {
  display: flex;
  gap: 24px; /* 增加间距 */
  width: 100%;
  max-width: 1200px; /* 恢复最大宽度限制，避免大屏过于分散 */
  margin: 0 auto; /* 居中 */
  padding: 0 20px;
  box-sizing: border-box;
}

.function-card {
  flex: 1; /* 让每个卡片平均分配宽度 */
  height: 550px; /* 增加卡片高度 */
  border-radius: 20px; /* 增加圆角，减少割裂感 */
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08); /* 柔和阴影 */
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.165, 0.84, 0.44, 1); /* 优化过渡效果 */
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(0,0,0,0.03); /* 微弱边框增加界限感 */
}

.function-card:nth-child(2) {
  height: 580px; /* 增加第二个卡片的高度 */
  margin-top: -15px;
}

.function-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.12);
}

.card-content {
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
}

.card-text {
  flex: 0 0 60%; /* 文字区域占据上半部分约60%高度 */
  padding: 30px 24px; /* 增加内边距 */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  position: relative;
  z-index: 2;
  background: #ffffff; /* 文字区域背景改为浅白色 */
  color: #2c3e50; /* 文字颜色改为深色以提高对比度 */
}

/* 移除了深灰遮罩层 */

.card-image {
  position: relative;
  width: 100%;
  flex: 0 0 40%; /* 图片区域占据下半部分约40%高度 */
  overflow: hidden;
}

.card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover; /* 图片铺满区域，确保不拉伸 */
  transition: transform 0.3s ease;
}

.function-card:hover .card-image img {
  transform: scale(1.05);
}

.overlay-title {
  font-size: 1.5rem;
  font-weight: bold;
  margin-bottom: 15px; /* 标题与副标题间距15px */
  color: #2E8B57; /* 深绿色粗体标题，匹配ecologi的品牌主色 */
}

.overlay-desc {
  font-size: 1rem;
  margin-bottom: 20px; /* 副标题与"了解更多"间距20px */
  color: #6c757d; /* 浅灰色常规字体副标题 */
}

.learn-more {
  color: #2E8B57; /* 深绿色文字 */
  text-decoration: none;
  font-weight: bold;
  position: relative;
}

.learn-more::after {
  content: '→';
  margin-left: 5px;
  transition: margin-left 0.3s ease;
}

.learn-more:hover::after {
  margin-left: 10px;
}

/* 产品/内容区 */
.products-section {
  padding: 40px 20px;
  background: #f0f8f0; /* 淡绿色背景 */
}

.products-container {
  max-width: 1200px;
  margin: 0 auto;
  position: relative;
}

.products-slider-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 0 50px;
}

.arrow-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #4CAF50;
  color: white;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.arrow-btn:hover:not(:disabled) {
  background: #45a049;
  transform: translateY(-50%) scale(1.1);
}

.arrow-btn:disabled {
  background: #9e9e9e;
  cursor: not-allowed;
  transform: translateY(-50%);
}

.left-arrow {
  left: 0;
}

.right-arrow {
  right: 0;
}

.products-slider {
  width: 100%;
  overflow: hidden;
  padding: 20px 0;
}

.product-card-container {
  display: flex;
  transition: transform 0.5s ease;
  width: 100%;
}

.product-card-item {
  flex: 0 0 33.33%;
  padding: 0 15px;
  box-sizing: border-box;
}

.product-card {
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  background: white;
}

.product-card.center-card {
  background: white;
  padding: 15px;
  border-radius: 20px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

.product-card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.product-info {
  padding: 20px;
}

.product-title {
  font-size: 1.3rem;
  font-weight: bold;
  margin-bottom: 10px;
  color: #2c3e50;
}

.product-desc {
  color: #6c757d;
  line-height: 1.6;
}

.indicators {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 20px;
}

.indicator-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #ccc;
  cursor: pointer;
  transition: all 0.3s ease;
}

.indicator-dot.active {
  background: #4CAF50;
  transform: scale(1.2);
}

.indicator-dot:hover {
  background: #45a049;
}

@media (max-width: 768px) {
  .product-card-item {
    flex: 0 0 100%;
  }
  
  .products-slider-wrapper {
    padding: 0 60px;
  }
  
  .arrow-btn {
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
  }
}
/* 社区/资讯区 */
.community-news-section {
  padding: 40px 20px;
  background: #f8f9fa;
  position: relative;
  min-height: 600px;
}

.news-container-full-bg {
  position: relative;
  width: 100%;
  height: 600px;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.news-bg-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.news-bg-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.news-content-overlay {
  position: absolute;
  bottom: 30px;
  right: 30px;
  width: 58%;
  background: #ffffff;
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.12);
  border: 1px solid rgba(0, 0, 0, 0.06);
  z-index: 2;
}

.news-tabs {
  display: flex;
  gap: 20px;
  margin-bottom: 30px;
  border-bottom: 2px solid #e9ecef;
}

.tab-btn {
  padding: 10px 20px;
  background: none;
  border: none;
  font-size: 1.1rem;
  font-weight: bold;
  color: #6c757d;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}

.tab-btn:hover {
  color: #4CAF50;
}

.tab-btn.active {
  color: #4CAF50;
}

.tab-btn.active::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  right: 0;
  height: 3px;
  background: #4CAF50;
  border-radius: 2px;
}

.news-content-wrapper {
  display: flex;
  flex-direction: column;
  gap: 16px;
  height: auto;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.news-content-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  gap: 12px;
  padding: 10px 0;
  box-sizing: border-box;
  min-height: 300px;
  opacity: 1;
  transition: opacity 0.3s ease;
}

.news-item {
  display: flex;
  gap: 16px;
  padding: 16px 20px;
  border-radius: 10px;
  transition: all 0.3s ease;
  cursor: pointer;
  align-items: center;
  width: 100%;
  box-sizing: border-box;
}

.news-item:hover {
  background: #f8f9fa;
}

.news-thumb {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  object-fit: cover;
}

.news-text {
  flex: 1;
}

.news-title {
  font-size: 1.1rem;
  font-weight: bold;
  margin-bottom: 10px;
  color: #2c3e50;
}

.news-meta {
  font-size: 0.9rem;
  color: #6c757d;
}

.polar-line {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 2px;
  height: var(--length);
  background: #4CAF50;
  transform-origin: top center;
  transform: translate(-50%, -100%) rotate(var(--rotation));
  border-radius: 0 0 2px 2px;
}

.carbon-chart {
  width: 80px;
  height: 80px;
  background: #f0f8f0;
  border-radius: 8px;
}

.carbon-chart-container {
  width: 100%;
  min-height: 360px;
  background: #f0f8f0;
  border-radius: 8px;
  padding: 20px;
  box-sizing: border-box;
}

/* 淡入淡出过渡效果 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 底部 */
.home-footer {
  background: #2c3e50;
  color: white;
  padding: 60px 20px 30px;
}

.footer-container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  gap: 40px;
}

.footer-info h3 {
  font-size: 1.5rem;
  margin-bottom: 20px;
  color: #4CAF50;
}

.footer-info p {
  margin-bottom: 15px;
  color: #ecf0f1;
  line-height: 1.6;
}

.footer-qrcodes {
  display: flex;
  gap: 30px;
}

.qrcode-item {
  text-align: center;
}

.qrcode-placeholder {
  width: 100px;
  height: 100px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  margin-bottom: 10px;
}

.qrcode-item p {
  margin: 0;
  color: #ecf0f1;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .incentive-container {
    flex-direction: column;
    gap: 40px;
  }
}

@media (max-width: 992px) {
  .carousel-section {
    padding: 45px 20px 55px;
  }
  
  .carousel-container {
    max-width: 1080px;
    height: 422px;
  }
  
  .slide-title {
    font-size: 2rem;
  }
  
  .nav-links {
    flex-wrap: wrap;
    justify-content: center;
    gap: 15px;
  }
  
  .functions-grid {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .function-card {
    flex: 0 0 31%;
    height: 450px;
  }
  
  .function-card:nth-child(2) {
    height: 480px;
    margin-top: -15px;
  }
  
  .section-title {
    font-size: 2.2rem;
  }
  
  .entrance-card {
    padding: 25px;
    min-height: 180px;
  }
  
  .card-icon {
    font-size: 2.5rem;
  }
  
  .card-title {
    font-size: 1.3rem;
  }
  
  .news-list {
    height: auto;
    min-height: 300px;
  }
  
  .news-content-area {
    min-height: 300px;
  }
}

  @media (max-width: 768px) {
    
    .btn {
      width: 100%;
      max-width: 250px;
    }
    
    .carousel-container {
      max-width: 960px;
      height: 375px;
    }
    
    .slide-title {
      font-size: 1.8rem;
    }
    
    .carousel-indicators {
      bottom: 20px;
      gap: 12px;
    }
    
    .indicator {
      width: 12px;
      height: 12px;
    }
    
    .indicator.active {
      width: 32px;
      border-radius: 8px;
    }
    
    .nav-container {
      flex-direction: column;
      height: auto;
      padding: 15px 20px;
    }
    
    .nav-links {
      margin-top: 15px;
    }
    
    .entrance-grid {
      grid-template-columns: 1fr;
      gap: 20px;
    }
    
    .section-title {
      font-size: 2rem;
      margin-bottom: 40px;
    }
    
    .section-header {
      flex-direction: column;
      align-items: flex-start;
    }
    
    .title-divider {
      width: 100%;
      min-width: unset;
      margin-top: 10px;
    }
    
    .entrance-card {
      padding: 20px;
      min-height: 160px;
    }
    
    .card-icon {
      font-size: 2.2rem;
    }
    
    .card-title {
      font-size: 1.2rem;
    }
    
    .card-desc {
      font-size: 0.95rem;
    }
    
    .action-buttons {
      flex-direction: column;
      align-items: center;
      gap: 30px;
    }
    
    .action-item {
      width: 100%;
      max-width: 250px;
    }
    
    .module-entrance-section {
      padding: 30px 15px;
    }
    
    .news-container {
      flex-direction: column;
    }
    
    .news-list {
      height: auto;
      min-height: 250px;
    }
    
    .news-content-area {
      min-height: 250px;
    }
    
    .functions-grid {
      flex-wrap: wrap;
      justify-content: center;
    }
    
    .function-card {
      flex: 1;
      height: 550px; /* 增加卡片高度 */
    }
    
    .function-card:nth-child(2) {
      height: 580px; /* 增加第二个卡片的高度 */
      margin-top: -15px;
    }
  }

@media (max-width: 480px) {
  
  .btn {
    padding: 12px 20px;
    font-size: 0.9rem;
  }
  
  .carousel-container {
    max-width: 720px;
    height: 281px;
  }
  
  .slide-title {
    font-size: 1.6rem;
  }
  
  .carousel-indicators {
    bottom: 15px;
    gap: 10px;
  }
  
  .indicator {
    width: 10px;
    height: 10px;
  }
  
  .indicator.active {
    width: 28px;
    border-radius: 8px;
  }
  
  .section-title {
    font-size: 1.8rem;
  }
  
  .entrance-card {
    padding: 18px;
    min-height: 150px;
    flex-direction: column;
    text-align: center;
    gap: 15px;
  }
  
  .card-icon {
    font-size: 2rem;
  }
  
  .card-title {
    font-size: 1.1rem;
  }
  
  .card-desc {
    font-size: 0.9rem;
  }
  
  .card-arrow {
    font-size: 1.2rem;
  }
  
  .points-amount {
    font-size: 3rem;
  }
  
  .module-entrance-section {
    padding: 20px 10px;
  }
  
  .news-list {
    min-height: 200px;
  }
  
  .news-content-area {
    min-height: 200px;
  }
  
  .functions-grid {
    gap: 15px;
  }
  
  .function-card {
    flex: 0 0 100%;
    height: 550px; /* 在小屏幕上也增加卡片高度 */
  }
  
  .function-card:nth-child(2) {
    height: 550px; /* 在小屏幕上也增加第二个卡片的高度 */
    margin-top: 0;
  }
}


/* 添加交换按钮样式 */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

/* 淡入淡出过渡效果 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
